﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mole
{
    class FileConfig
    {
        String Type { get; set; }
        String Extension { get; set; }
        String Query { get; set; }

        public static Boolean ExtensionMisMatch(String extension, String filetype)
        {
            Boolean value = false;
            List<FileConfig> listcfg = ParseFileConfig(System.Windows.Forms.Application.StartupPath + "\\FileSettings.cfg");

            foreach (FileConfig cfg in listcfg)
            {
                if (cfg.Extension.Trim('.') == extension)
                {
                    if (filetype.Contains(cfg.Query))
                    {
                        value = true;
                        break;
                    }
                }
            }

            return value;
        }

        public static Boolean isMatch(String extension, String filetype, String filequery)
        {
            Boolean value = false;

            List<FileConfig> listcfg = ParseFileConfig();
            foreach (FileConfig cfg in listcfg)
            {
                //look up if description matches what I've extracted
                if (filequery.Contains(cfg.Query))
                {
                    if (String.Equals(cfg.Type, filetype, StringComparison.OrdinalIgnoreCase))
                    {
                        value = true;
                        break;
                    }
                }
                else if (cfg.Query.Equals("*"))
                {
                    //look up by wildcard
                    if (String.Equals(extension.Trim('.'), cfg.Extension, StringComparison.OrdinalIgnoreCase))
                    {
                        value = true;
                        break;
                    }
                }
            }
            return value;
        }

        public static List<FileConfig> ParseFileConfig(String filepath)
        {
            List<FileConfig> filecfg = new List<FileConfig>();

            if (String.IsNullOrEmpty(filepath))
            {
                throw new System.ArgumentException("Parameter cannot be null or empty", "filepath");
            }
            else
            {
                if (System.IO.File.Exists(filepath))
                {
                    foreach (String line in System.IO.File.ReadAllLines(filepath))
                    {
                        if (!line.Contains('#') && (!String.IsNullOrEmpty(line)))
                        {
                            FileConfig linecfg = new FileConfig();

                            String[] splitcfg = line.Split(',');

                            linecfg.Type = splitcfg[0];
                            linecfg.Extension = splitcfg[1];
                            linecfg.Query = splitcfg[2];

                            filecfg.Add(linecfg);
                        }
                    }
                }
            }
            return filecfg;
        }

        public static List<FileConfig> ParseFileConfig()
        {
            return ParseFileConfig(System.Windows.Forms.Application.StartupPath + "\\FileSettings.cfg");
        }

    }
}
